//
//  MyReceiveAddressViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/18.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MyReceiveAddressViewController.h"


@interface MyReceiveAddressViewController ()<UITextViewDelegate>

@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UIButton *submitBtn;

@end

//网络
@implementation MyReceiveAddressViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId,
                                 @"auAddress":self.textView.text};
    [[AFAppDotNetAPIClient sharedClient] POST:@"register/updateauAddress.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            [self.view makeToast:responseObject[@"msg"] duration:1 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                [UserModel sharedInstanced].address = self.textView.text;
                if (self.getAddress) {
                    self.getAddress(self.textView.text);
                }
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation MyReceiveAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的收货地址";
    [self.view addSubview:self.textView];
    [self.view addSubview:self.submitBtn];
    if (![[UserModel sharedInstanced].address isEqualToString:@"未填写"]) {
        self.textView.text = [UserModel sharedInstanced].address;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
}

#pragma mark - Event

- (void)keyboardWasShown:(NSNotification*)aNotification{
    CGRect keyBoardFrame = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
        _submitBtn.center = CGPointMake(SCREEN_WIDTH/2., SCREEN_HEIGHT-64 - 60 - keyBoardFrame.size.height);
    }];
}

-(void)keyboardWillBeHidden:(NSNotification*)aNotification{
    CGFloat duration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
        _submitBtn.center = CGPointMake(SCREEN_WIDTH/2., SCREEN_HEIGHT-64 - 60);
    }];
}

- (void)submitEvent{
    [self getData];
}

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"请输入收货地址"]) {
        _textView.text = @"";
        _textView.textColor = [UIColor blackColor];
    }
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@""]) {
        _textView.text = @"请输入收货地址";
        _textView.textColor = [UIColor grayColor];
    }
    return YES;
}

#pragma mark - set & get

- (UITextView *)textView{
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH-10, SCREEN_HEIGHT - 64 - 200)] ;
        _textView.delegate = self;
        _textView.textColor = [UIColor grayColor];
        _textView.text = @"请输入收货地址";
    }
    return _textView;
}

- (UIButton *)submitBtn{
    if (!_submitBtn) {
        _submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
        _submitBtn.center = CGPointMake(SCREEN_WIDTH/2., SCREEN_HEIGHT-64 - 60);
        _submitBtn.backgroundColor = getColor(mainColor);
        _submitBtn.layer.masksToBounds = YES;
        _submitBtn.layer.cornerRadius = 15;
        _submitBtn.titleLabel.font = DEF_FontSize_12;
        [_submitBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_submitBtn addTarget:self action:@selector(submitEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _submitBtn;
}

@end
